package com.ease.integration.exception;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPFault;
import javax.xml.ws.WebFault;
import javax.xml.ws.soap.SOAPFaultException;

import com.ease.integration.constant.TargetNamespace;
import com.ease.integration.message.ServiceFault;

@WebFault(name = "serviceFault")
public class ServiceException extends Exception {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1836487156081081290L;

	private ServiceFault serviceFault;

	public ServiceException() {
		super();
		this.serviceFault = new ServiceFault();
	}

	public ServiceException(Throwable cause) {
		super(cause);
		this.serviceFault = new ServiceFault();
	}

	public ServiceException(String message, Throwable cause) {
		super(message, cause);
		this.serviceFault = new ServiceFault(message);
	}

	public ServiceException(String message, ServiceFault serviceFault) {
		super(message);
		this.serviceFault = serviceFault;
	}

	public ServiceException(ServiceFault serviceFault) {
		super();
		this.serviceFault = serviceFault;
	}

	public ServiceException(String errorCode) {
		super();
		this.serviceFault = new ServiceFault(errorCode);
	}

	public ServiceException(String errorCode, String errorMessage) {
		super();
		this.serviceFault = new ServiceFault(errorCode, errorMessage);
	}

	/**
	 * http://www.cnblogs.com/huqingyu/archive/2010/03/23/1692530.html
	 * 
	 * 自定义的Exception必需要实现getFaultInfo()方法,假设返回的类型是A,还必需要有以String和A对象为参数的构造方法。
	 * 
	 * @return
	 */
	// public ServiceFault getFaultInfo() {
	// return this.serviceFault;
	// }
	public Throwable getCause() {
		try {
			SOAPFault fault = SOAPFactory.newInstance().createFault();
			fault.setFaultCode(new QName(TargetNamespace.NAMESPACE, this.getServiceFault().getErrorCode()));
			fault.setFaultString(this.getServiceFault().toString());
			SOAPFaultException ex = new SOAPFaultException(fault);
			return ex;
		} catch (Exception e) {
			e.printStackTrace();
			return super.getCause();
		}
	}

	/**
	 * @return the serviceFault
	 */
	public ServiceFault getServiceFault() {
		return serviceFault;
	}

	/**
	 * @param serviceFault
	 *            the serviceFault to set
	 */
	public void setServiceFault(ServiceFault serviceFault) {
		this.serviceFault = serviceFault;
	}

}